<!DOCTYPE html>
<html lang="zh">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<link rel="stylesheet" type="text/css" href="./base.css">
	<title></title>
	
	<style>
		html {
			font-size: 50px;
		}
		.main-container {
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			padding-top: 0.1rem;
		}
		.order-info {
			width: 100%;
			padding: 0.3rem 0.5rem;
			line-height: 0.7rem;
			font-size: 0.3rem;
		}
		.order-item:first-child {
			color: red;
			margin-top: 0.1rem;
			line-height: 0.5rem;
			font-weight: bold;
		}
		#btn {
			margin-top: 0.2rem;
			width: 50%;
			line-height: 0.9rem;
			text-align: center;
			font-size: 0.32rem;
			color: #fff;
			border-radius: 0.45rem;
			background-color: #04BE02;
		}
	</style>
	<script type="text/javascript" charset="UTF-8" src="https://wx.gtimg.com/pay_h5/goldplan/js/jgoldplan-1.0.0.js"></script>
</head>
<body>
	
	<div class="main-container" id="result">
		<div class="order-info">
			<p class="order-item center" id="payTimer"><span id="timer"></span>s后自动返回商户</p>
			<p class="order-item status" id="payStatus"></p>
			<p class="order-item">订单编号：<span id="merOrderId"></span></p>
			<p class="order-item">支付时间：<span id="payTime"></span></p>
			<p class="order-item">支付金额：<span id="amt"></span>元</p>
		</div>
		<button class="back-btn" onclick="jumpOut()" id="btn">返回商家</button>
	</div>
	
	<script>
		var sHref = window.location.href
		sHref = sHref.replaceAll('&amp;', '&')
		function getArgFromHref (paramName) {
			// var args = sHref.split('?')
			// var retval = ''
			// if (args[0] === sHref) {
			// 	return retval /* 无需做任何处理 */
			// }
			// var str = args[1]
			// args = str.split('&')
			// for (var i = 0; i < args.length; i++) {
			// 	str = args[i]
			// 	var arg = str.split('=')
			// 	if (arg.length <= 1) {
			// 		continue
			// 	}
			// 	if (arg[0] === sArgName) {
			// 		retval = arg[1]
			// 	}
			// }
			// if (retval.indexOf('/#') !== -1) {
			// 	retval = retval.split('/#')[0]
			// } else if (retval.indexOf('#') !== -1) {
			// 	retval = retval.split('#')[0]
			// }
			// return retval
			var paramStringArr = sHref.substring(sHref.indexOf('?') + 1).split('&');
			for (var i = 0; i < paramStringArr.length; i++) {
				var paramString = decodeURIComponent(paramStringArr[i].replace('+', '%20'));
				var equalIndex = paramString.indexOf('=');
				var name = paramString.substring(0, equalIndex).toLowerCase();
				if (trim(name) == trim(paramName.toLowerCase())) {
					return paramString.substring(equalIndex + 1)
				}
			}
			return ''
		}
		
		function trim(str) { //删除左右两端的空格
			return str.replace(/(^\s*)|(\s*$)/g, "");
		}
		
		function jumpOut () {
			var parentUrl = getParentUrl();
			if (null != parentUrl && parentUrl.indexOf('weixin') > 0) {
				var mchDataJump = {
					action: 'jumpOut',
					jumpOutUrl: jumpOutUrl
				}
				var postDataJump = JSON.stringify(mchDataJump);
				parent.postMessage(postDataJump, 'https://payapp.weixin.qq.com');
			} else {
				location.replace(jumpOutUrl);
			}
		}
		
		function getParentUrl() {
			var url = null;
			if (parent !== window) {
				try {
					url = parent.location.href;
				} catch (e) {
					url = document.referrer;
				}
			}
			return url;
		}
		
		function show_time(g_timer) {
			setInterval(v=>{
				if (g_timer > 0) {
					g_timer--;
					timer = g_timer
					let timerEl = document.getElementById('timer')
					timerEl.innerText = timer
				} else {
					jumpOut();
					return
				}
			}, 1000);
		}
		
		var mchData = {
			action: 'onIframeReady',
			displayStyle: 'SHOW_CUSTOM_PAGE'
		};
		var postData = JSON.stringify(mchData);
		parent.postMessage(postData, 'https://payapp.weixin.qq.com');
		
		var timer = 5;
		var url = window.location.href;
		var baseUrl = url.substring(0, url.indexOf("static"));
		var currentHref = window.location.href
		var merOrderId = getArgFromHref('orderId');
		var payTime = getArgFromHref('payTime');
		var getAmt = getArgFromHref('buyerPayAmount');
		var status = getArgFromHref('status')
		var amt = Number(getAmt / 100).toFixed(2);
		var pageType = getArgFromHref('pageType');
		var totalAmount = getArgFromHref('totalAmount')
		totalAmount = Number(totalAmount / 100).toFixed(2);
		
		var jumpOutUrl = '';
		
		if (pageType === 'mall') {
			jumpOutUrl = baseUrl + 'subPackages/orderPkg/pages/orderDetail/orderDetail?orderId=' + merOrderId;
		} else if (pageType === 'profitsOrder') {
			if (status === 'TRADE_SUCCESS') {
				jumpOutUrl = baseUrl + 'subPackages/right/pages/profitsPayResult/profitsPayResult?id=' + merOrderId
			} else {
				jumpOutUrl = baseUrl + 'subPackages/right/pages/myProfitsOrderDetail/myProfitsOrderDetail?id=' + merOrderId
			}
		} else if (pageType == 'couponBag') {
			let couponBagId = getArgFromHref('couponBagId')
			if (status == 'TRADE_SUCCESS') {
				jumpOutUrl = baseUrl + 'subPackages/minorPkg/pages/couponPayResult/couponPayResult?id=' + couponBagId + '&orderId=' + merOrderId + '&totalAmount=' + totalAmount
			} else {
				jumpOutUrl = baseUrl + 'subPackages/minorPkg/pages/couponBag/couponBag?id=' + couponBagId
			}
		} else if (pageType == 'couponOrder') {
			let orderType = getArgFromHref('orderType')
			if (status == 'TRADE_SUCCESS') {
				if ('groupbuy' == orderType || 6 == orderType) {
					jumpOutUrl = baseUrl + 'couponMarketingPkg/pages/couponGroupBuy/groupbuyOrderDetail/groupbuyOrderDetail?payId=' + merOrderId
				} else {
					jumpOutUrl = baseUrl + 'pages/coupon/myCouponDetail/myCouponDetail?payId=' + merOrderId
				}
			} else {
				jumpOutUrl = baseUrl + 'pages/coupon/myCouponDetail/myCouponDetail?payId=' + merOrderId
			}
		}
		console.log(jumpOutUrl)
		
		
		var resultEl = document.getElementById('result')
		var loadingEl = document.getElementById('loading')
		var payStatusEl = document.getElementById('payStatus')
		var payTimeEl = document.getElementById('payTime')
		var amtEl = document.getElementById('amt')
		var merOrderIdEl = document.getElementById('merOrderId')
		payStatusEl.innerText = status === 'TRADE_SUCCESS' ? '支付成功' : '支付失败'
		payTimeEl.innerText = payTime
		amtEl.innerText = totalAmount
		merOrderIdEl.innerText = merOrderId
		
		if (jumpOutUrl) {
			show_time(timer)
		}
	</script>
</body>
</html>